package com.tywj.buscustomerapp.view.activity;

import android.Manifest;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tywj.buscustomerapp.R;
import com.tywj.buscustomerapp.utils.PermissionsChecker;
import com.tywj.buscustomerapp.utils.ToastUtils;
import com.tywj.buscustomerapp.view.adapter.GuideViewPagerAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class FirstComeActivity extends BaseActivity {
    @BindView(R.id.welcome_viewpager)
    ViewPager viewPager;
    @BindView(R.id.btn)
    ImageView btn;
    @BindView(R.id.radio)
    RadioGroup radioGroup;
    private static final int[] pics = { R.layout.velcome_view1_layout, R.layout.velcome_view2_layout, R.layout.velcome_view3_layout};
    private GuideViewPagerAdapter adapter;
    private List<View> views;
    private PermissionsChecker mPermissionsChecker;
    //权限集合
    static final String[] PERMISSIONS = {
            Manifest.permission.ACCESS_FINE_LOCATION,
    };
    private static final int PERMISSIONS_REQUEST_STORAGE = 1;
    @Override
    protected int getContentId() {
        return R.layout.activity_first_come;
    }

    @Override
    protected void initWidget(Bundle savedInstanceState) {
        super.initWidget(savedInstanceState);
        if (mPermissionsChecker == null){
            mPermissionsChecker = new PermissionsChecker(this);
        }        if (mPermissionsChecker == null){
            mPermissionsChecker = new PermissionsChecker(this);
        }
        if (mPermissionsChecker.lacksPermissions(PERMISSIONS)){
            ActivityCompat.requestPermissions(FirstComeActivity.this, PERMISSIONS,PERMISSIONS_REQUEST_STORAGE);
        }
        views = new ArrayList<View>();
        for (int i = 0; i < pics.length; i++) {
            View view = LayoutInflater.from(FirstComeActivity.this).inflate(pics[i], null);
            views.add(view);
        }
        // 初始化adapter
        adapter = new GuideViewPagerAdapter(views);
        viewPager.setAdapter(adapter);
        final int[] ids = {R.id.radio1,R.id.radio2,R.id.radio3};
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position!=(ids.length-1)){
                    btn.setVisibility(View.GONE);
                }else{
                    btn.setVisibility(View.VISIBLE);
                }
                radioGroup.check(ids[position]);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode){
            case PERMISSIONS_REQUEST_STORAGE: {
                // 如果取消权限，则返回的值为0
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                } else {
                    ToastUtils.show("请开启定位权限");
                }
                return;
            }
        }
    }
}
